package com.caseProcess.service.impl;

import com.caseProcess.entity.Case;
import com.caseProcess.entity.CaseOperNote;
import com.caseProcess.entity.CaseProcInfo;
import com.caseProcess.mapper.*;
import com.caseProcess.service.CaseService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CaseServiceImpl implements CaseService {

    @Resource
    private CaseDao caseDao;
    @Resource
    private ResourcesDao resourcesDao;
    @Resource
    private CaseOperNoteDao caseOperNoteDao;
    @Resource
    private CaseProcInfoDao caseProcInfoDao;
    @Resource
    private UrgingDao urgingDao;

    @Override
    public List<Case> selectCases(Case cases) {
        return caseDao.selectCases(cases);
    }

    @Override
    public int batchInsert(List<Case> list) {
        return caseDao.batchInsert(list);
    }

    @Override
    public List<Case> selectCases(Case cases, String endTime) {
        return null;
    }

    @Override
    public int insert(Case cases) {
        return caseDao.insert(cases);
    }

    @Override
    public int update(Case cases) {
        return caseDao.update(cases);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(String caseId) throws Exception {
        List<CaseProcInfo> list=caseProcInfoDao.selectCaseProcInfoByCaseId(caseId);
        if(list.isEmpty()){
            try {
                caseDao.delete(caseId);
            }catch (Exception e){
                throw e;
            }
            return true;
        }
        CaseProcInfo caseProcInfo=list.get(0);
        CaseOperNote caseOperNote=new CaseOperNote();
        caseOperNote.setCaseProcInfo(caseProcInfo);
        caseOperNote.setResources(true);
        List<CaseOperNote> caseOperNoteList=caseOperNoteDao.selectCaseOperNotes(caseOperNote);
        if(caseOperNoteList.isEmpty()){
            try {
                urgingDao.deleteByProcessId(caseProcInfo.getProcessId());//删除催办表
                caseOperNoteDao.deleteByProcessId(caseProcInfo.getProcessId());//删除处理过程
                caseProcInfoDao.deleteByCaseId(caseId);//删除分派过程
                caseDao.delete(caseId);//删除警情
            }catch (Exception e) {
                throw e;
            }
        }else {
            List<String> caseOperNoteIds=new ArrayList<String>();
            for(int i=0;i<list.size();i++){
                caseOperNoteIds.add(caseOperNoteList.get(i).getOperNoteId());
            }
            try {
                resourcesDao.deleteByopernoteIds(caseOperNoteIds);//删除资源表
                urgingDao.deleteByProcessId(caseProcInfo.getProcessId());//删除催办表
                caseOperNoteDao.deleteByProcessId(caseProcInfo.getProcessId());//删除处理过程
                caseProcInfoDao.deleteByCaseId(caseId);//删除分派过程
                caseDao.delete(caseId);//删除警情
            } catch (Exception e) {
                throw e;
            }
        }
        return true;
    }


}
